﻿using System;
using System.Web.Security;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;
using SimpleScrum.Areas.Scrum.Models;
using SimpleScrum.Core.Entities;
using SimpleScrum.Core.Mappings;

namespace SimpleScrum.Helpers
{
    public class NHibernateHelper
    {
        private static ISessionFactory SessionFactory { get; set; }

        public static ISessionFactory GetCurrentFactory()
        {
            //if (SessionFactory == null)
            {
                SessionFactory = CreateSessionFactory();
            }
            return SessionFactory;
        }

        private static ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                           .Database(MsSqlConfiguration.MsSql2012.ConnectionString(
                                             c => c.FromConnectionStringWithKey("SimpleScrumDatabaseConnection")))
                //.Mappings(m => m.FluentMappings.Add(typeof (UserProfileMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (UsersMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (OrganizationMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (ProjectMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (TimeLineMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (ProjectUserMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (TeamMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (TeamMemberMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (UserStoryMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (SprintMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof (BackLogMap)))
                           .Mappings(m => m.FluentMappings.Add(typeof(UserStoryTaskMap)))
                //.Mappings(m => m.FluentMappings.Add(typeof(WebpagesMembershipMap)))
                //.Mappings(m => m.FluentMappings.Add(typeof(WebpagesOAuthMembershipMap)))
                //.Mappings(m => m.FluentMappings.Add(typeof(WebpagesRolesMap)))
                //.Mappings(m => m.FluentMappings.Add(typeof(WebpagesUserInRolesMap)))
                           .BuildSessionFactory();
        }

        public static ISession GetSession()
        {
            return GetCurrentFactory().OpenSession();
        }

        public static void DatabaseInitialize()
        {
            try
            {
                var usersModel = new UsersModel();
                usersModel.GetUsersList();
            }
            catch (Exception)
            {
                //if (GetSession() == null)
                {
                    Fluently.Configure()
                            .Database(MsSqlConfiguration.MsSql2012.ConnectionString(
                                              c => c.FromConnectionStringWithKey("SimpleScrumDatabaseConnection")))
                        //.Mappings(m => m.FluentMappings.Add(typeof (UserProfileMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (UsersMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (OrganizationMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (ProjectMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (TimeLineMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (ProjectUserMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (TeamMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (TeamMemberMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (UserStoryMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (SprintMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof (BackLogMap)))
                            .Mappings(m => m.FluentMappings.Add(typeof(UserStoryTaskMap)))
                        //.Mappings(m => m.FluentMappings.Add(typeof (WebpagesMembershipMap)))
                        //.Mappings(m => m.FluentMappings.Add(typeof (WebpagesOAuthMembershipMap)))
                        //.Mappings(m => m.FluentMappings.Add(typeof (WebpagesRolesMap)))
                        //.Mappings(m => m.FluentMappings.Add(typeof (WebpagesUserInRolesMap)))
                            .ExposeConfiguration(cfg => new SchemaExport(cfg).Execute(true, true, false))
                            .BuildConfiguration().BuildSessionFactory();
                }
                Roles.CreateRole("user");
                Roles.CreateRole("admin");
            }
        }
    }
}